package com.kongzhong.test;

import com.alibaba.fastjson.JSONObject;
import com.sun.swing.internal.plaf.basic.resources.basic;

public class QueueTest01 implements Runnable {

	public static WebAccess webAccess = new WebAccess();
	public static String url = "http://192.168.131.61:8080/QueueTest01/queueController?type=getQueueSize";
	public static String url1 = "http://192.168.131.61:8080/QueueTest01/queueController?type=offerObj";
	public static String url2 = "http://192.168.131.61:8080/QueueTest01/queueController?type=pollObj";

	@Override
	public void run() {
		String str = null;
		while (getBoolean()) {
			str = webAccess.getUrlContent(url1, 1000, 1000, "UTF-8");
			try {
				if (str != null) {
					str = str.substring(5, str.length());
					str = str.substring(0, str.length() - 1);
					JSONObject jsonObject = JSONObject.parseObject(str);
					boolean flag = jsonObject.getBooleanValue("result");
					if (!flag) {
						break;
					}
				} else {
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
				break;
			}
		}
		System.out.println(Thread.currentThread().getName() + " end");
	}

	public static synchronized boolean getBoolean() {
		String str = webAccess.getUrlContent(url, 1000, 1000, "UTF-8");
		boolean result = false;
		boolean flag = false;
		int size = 0;
		if (str != null) {
			// null({"info":364,"result":true})
			str = str.substring(5, str.length());
			str = str.substring(0, str.length() - 1);
			JSONObject jsonObject = JSONObject.parseObject(str);
			flag = jsonObject.getBooleanValue("result");
			size = jsonObject.getIntValue("info");
		}
		if (flag && size <= 150000) {
			result = true;
		}
		return result;
	}

	public static void main(String[] args) {
//		QueueTest01 queueTest01 = new QueueTest01();
//		for (int i = 0; i < 200; i++) {
//			Thread thread = new Thread(queueTest01, "thread" + i);
//			thread.start();
//		}
//		System.out.println("end");
		
		for (int i = 0; i < 10; i++) {
			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					while (true) {
						webAccess.getUrlContent(url2, 1000, 1000, "UTF-8");
					}
				}
			});
			thread.start();
		}
		System.out.println("end");

	}

}
